<?php

/**
 * Validate if given last.fm account exists
 *
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';

class LastFM_ValidateUser extends Zend_Validate_Abstract
{
    const INVALID = 'fmInvalid';

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::INVALID => 'Last.fm account does not exists',
    );

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if account exists
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {

        try {
            $user = new LastFM_User($value);
            $user->getInfo();
        }
        catch (LastFM_Exception $e) {
            $this->_error(self::INVALID);
            return false;
        }
       return true;
    }

}
